package nettyTest.netty4test1.decode;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class SubReqClientHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        for (int i = 0; i < 10; i++) {
            ctx.write(req(i));
        }
        ctx.flush();
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        SubscribeResp subscribeResp = (SubscribeResp) msg;
        System.out.println(subscribeResp);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }

    public SubscribeReq req(int id) {
        SubscribeReq subscribeReq = new SubscribeReq();
        subscribeReq.setSubReqId(id);
        subscribeReq.setUserName("name" + id);
        subscribeReq.setAddress("add" + id);
        subscribeReq.setPhoneNumber("ph" + id);
        subscribeReq.setProductName("prob" + id);
        return subscribeReq;
    }
}
